
local HUD = {}


function HUD:initialize()

	self.viewport = MOAIViewport.new()

	self.viewport:setSize( SCREEN_RESOLUTION_X, SCREEN_RESOLUTION_Y )
	self.viewport:setScale( SCREEN_RESOLUTION_X, -SCREEN_RESOLUTION_Y )
	self.viewport:setOffset( -1, 1 )

	-- create layer
	self.layer = MOAILayer2D.new()
	self.layer:setViewport( self.viewport )
	--self.layer:setCamera( Game.camera )
	-- render the layer
	--Game.render_layer(self.layer)
	
	local renderTable = MOAIRenderMgr.getRenderTable()
	table.insert( renderTable, self.layer )
	MOAIRenderMgr.setRenderTable ( renderTable )
	

	self.font = MOAIFont.new()
	self.font = ResourceManager:get( "hudFont" )
	self.size = 16

	self.console_log = {}

	self.style = MOAITextStyle.new()
	self.style:setFont( self.font )
	self.style:setSize( self.size )
	self.style:setColor( 255, 53, 171 )
	self.initial = true

	self:add_console( GAME_NAME )
	self:add_console( "v."..GAME_VERSION)
	self:add_console( GAME_AUTHOR )



	self:initializeDebugHud ()
	
	self:draw_gui()
	--self:draw_uts()

end

function HUD:initializeDebugHud()


	--self.leftRightIndicator = self:newDebugTextBox( 30, {10, 10, 100, 50 } )
	--self.positionIndicator = self:newDebugTextBox( 30, {10, 50, 200, 100 } )

	self.newInd = self:display_text( { 10, 80, 100, 100 }, self.style )

	--self:display_maps()

end

function HUD:display_console()

	self.console = {}
	self.console.textbox = self:display_text ( {MAP_DRAW_OFFSET_X, MAP_DRAW_OFFSET_Y,
		MAP_DRAW_OFFSET_X + 500, MAP_DRAW_OFFSET_Y+500 }, self.style)
	self.console.text = ""




end

function HUD:display_text ( rect, style )

	local text_box = MOAITextBox.new ()

	print("HUD text_box from ", rect[1]..","..rect[2], " to ",
		rect[3]..","..rect[4])

	text_box:setRect(unpack(rect))
	text_box:setStyle(style)

	self.layer:insertProp( text_box)

	return text_box

end

function HUD:draw_uts()

	local base_width = 9
	local base_height = 3
	local base_x = 90
	local base_y = 1

	self.uts, self.uts_id = GuiManager:new_popup("window",
		base_x, base_y, base_width, base_height, TM.uts)
	print("HUD draw_uts TM.uts = " .. TM.uts)
	self.uts:setBackgroundImage("assets\\8metal.png")
	self.uts:setTextAlignment(self.uts.TEXT_ALIGN_LEFT,
		self.uts.TEXT_ALIGN_CENTER)
		
	self.day, self.day_id = GuiManager:new_popup("window",
		base_x, base_y+base_height+base_y, base_width, base_height, TM.day)
		
	self.day:setBackgroundImage("assets\\8metal.png")
	self.day:setTextAlignment(self.day.TEXT_ALIGN_LEFT,
		self.day.TEXT_ALIGN_CENTER)
	
	

end

function HUD:draw_gui()

	
	
	local top_layer = #MOAIRenderMgr.getRenderTable()+1
	
	if self.initial == true then

		gui:addToResourcePath("engine/gui/resources/fonts")
		gui:addToResourcePath("engine/gui/resources/gui")
		gui:addToResourcePath("engine/gui/resources/media")
		gui:addToResourcePath("engine/gui/resources/themes")
	end

	--layermgr.addLayer("gui", 99999, gui:layer())
	Game:render_layer(gui:layer(), nil, top_layer)
	
	if self.initial == true then
		gui:setTheme("basetheme.lua")
		gui:setCurrTextStyle("default")
	end

	--label1 = gui:createLabel()
	--label1:setPos(0, 75)
	--label1:setDim(100, 25)
	--label1:setText("It's a label")
	--label1:setTextAlignment(label1.TEXT_ALIGN_CENTER)
	
	
	local a
	
	if self.initial then self.initial = false end
	


end


function HUD:display_maps()

	local text = MapManager:list_map_files()
	local size = 30
	local offset_x, offset_y = MAP_DRAW_OFFSET_X, MAP_DRAW_OFFSET_Y
	self.d_maps = {}


	for i = 1, #text do
		--print(text[i])
		local t_len = text[i]:len()
		local ul_x = offset_x
		local ul_y = offset_y+i*size
		local lr_x = offset_x+t_len*size
		local lr_y = offset_y+size+i*size
		local rect = { ul_x, ul_y, ul_x+200, lr_y }
		local iter = #self.d_maps+1

		self.d_maps[iter] = self:display_text( rect, self.style )

	end

	for i = 1, #self.d_maps do

		self.layer:insertProp( self.d_maps[i] )

	end

end

function HUD:remove_console_display()

	self.layer:removeProp(self.console.textbox)

	self.console = nil


end

function HUD:remove_map_display()
	if self.d_maps then
		for i = 1, #self.d_maps do

			self.layer:removeProp(self.d_maps[i])
		end
		
		self.d_maps = nil
	end
	
	

end

function HUD:assemble_console()

	local con_str = ""

	for i = #self.console_log, 1, -1 do

		con_str = con_str .. self.console_log[i]

	end
	
	if self.console then
		if self.console.text then
			if self.console.text ~= '~' then
				con_str = self.console.text .. con_str
			end
		end
	end

	return con_str

end

function HUD:add_console( text )

	local iter = #self.console_log+1
	
	local txt = text:gsub("~", "")

	self.console_log[iter] = "\n"..txt
	
	self:parse_console( txt )

end

function HUD:parse_console( text )

	local pri = text:match("^([%a]+)%s")
	local second = text:match("%s([%w]+)%s")
	local tri = text:match("%s([%w]+)$")
	
	if pri then print("HUD console pri : " .. pri ) end
	if second then print("HUD console second : " .. second ) end
	if tri then print("HUD console tri : " .. tri ) end
	
	
	
	console_actions = {
		['map'] = function( self, second, tri )
			print("HUD console mappy")
		
			HUD:remove_map_display()
			if second then 
				MapManager:load_map(second, tri, 1)
			elseif tri then
				MapManager:load_map(USER_NAME, tri, 1)
			else
				print("HUD console invalid map")
			end
		
		
		end,
	
	
	}
	if console_actions[pri] then
		console_actions[pri](self, second, tri)
	end

	

end

function HUD:select_map( )

	local x, y = InputManager:position()
	--print(x, y)

	local maps = self.d_maps

	for i = 1, #maps do

		local tx1, ty1, tx2, ty2 = maps[i]:getRect()

		--print(tx1, ty1, tx2, ty2)

		if x > tx1 and y > ty1
			and x < tx2 and y < ty2 then
			print("HUD select map map_num : ".. i)
			return i

		end

	end

end

--[[
function HUD:select_textbox( textbox_table )

	if InputManager:isDown() then
		local x, y = InputManager:position()

		print("click at : " .. x .. " " .. y )

		for i = 1, #textbox_table do

			local tx1, ty1, tx2, ty2 = textbox_table[i]:getRect()

			if x > tx1 and y > tx2
				and x < tx2 and y < ty2 then

				return i

			end

		end

	end

	return nil

end
]]

function HUD:newDebugTextBox( size, rectangle )

	local textBox = MOAITextBox.new ()
	textBox:setStyle(self.style)
	textBox:setRect( unpack(rectangle) )
	self.layer:insertProp( textBox )

	return textBox

end



function HUD:update ()

	if self.uts then
	
		self.uts:setText(" " .. TM.uts .. " UTS")
	
	end
	
	if self.day then
		
		self.day:setText(" Day " .. TM.day)
	
	end


	if self.d_maps then
		local map_text = MapManager:list_map_files()
		for i = 1, #self.d_maps do
			--print(map_text[i])
			self.d_maps[i]:setString(map_text[i])
		end

	end

	if self.console then

		local str = HUD:assemble_console()
		self.console.textbox:setString(str)

	end

	local x, y = Game.camera:getLoc()
	--[[
	if x > 0 then
		self.leftRightIndicator:setString( "Right" )
	else
		self.leftRightIndicator:setString( "Left" )
	end

	self.newInd:setString("doobie")

	--x, y = Character.physics.body:getPosition()
	self.positionIndicator:setString("(" ..
	math.floor(x) .. ", " ..
	math.floor(y) .. ")")
	]]



end


return HUD
